package com.qf.driver.service.impl;

import com.alibaba.fastjson.JSON;
import com.qf.common.bean.R;
import com.qf.common.dto.DeviceDto;
import com.qf.common.dto.DriverMqttDto;
import com.qf.common.dto.ProductDto;
import com.qf.common.model.QfDevice;
import com.qf.common.model.QfProduct;
import com.qf.driver.feign.DeviceFeign;
import com.qf.driver.feign.ProductFeign;
import com.qf.driver.mqtt.bean.MqttProperties;
import com.qf.driver.service.AuthService;
import com.qf.driver.service.DeviceTopicService;
import com.qf.driver.service.MqttUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 千锋健哥
 */
@Service
@Slf4j
public class AuthServiceImpl implements AuthService {

    @Autowired
    private ProductFeign productFeign;

    @Autowired
    private DeviceFeign deviceFeign;

    @Autowired
    private MqttUserService mqttUserService;

    @Autowired
    private DeviceTopicService deviceTopicService;

    @Autowired
    private MqttProperties mqttProperties;

    @Override
    public R<DriverMqttDto> login(DriverMqttDto dto) {
        //1. 校验产品key是否正确
        ProductDto productDto = new ProductDto();
        productDto.setProductKey(dto.getProductKey());
        R<List> productResult = productFeign.findAreaAll(productDto);
        if (productResult == null) {
            log.info("==========产品key错误========");
            return R.fail("产品key错误");
        }
        if (productResult.getData() == null) {
            log.info("==========产品key错误========");
            return R.fail("产品key错误");
        }
        if (productResult.getData().size() == 0) {
            log.info("==========产品key错误========");
            return R.fail("产品key错误");
        }

        //2. 校验设备key是否正确
        DeviceDto deviceDto = new DeviceDto();
        deviceDto.setDeviceKey(dto.getDeviceKey());
        R<List> deviceResult = deviceFeign.findAreaAll(deviceDto);
        if (deviceResult == null) {
            log.info("==========设备key错误========");
            return R.fail("设备key错误");
        }
        if (deviceResult.getData() == null) {
            log.info("==========设备key错误========");
            return R.fail("设备key错误");
        }
        if (deviceResult.getData().size() == 0) {
            log.info("==========设备key错误========");
            return R.fail("设备key错误");
        }

        //3. 校验设备秘钥是否正确
        String jsonStr = JSON.toJSONString(deviceResult.getData().get(0));
        QfDevice device = JSON.parseObject(jsonStr, QfDevice.class);
        if (!device.getDeviceSecret().equals(dto.getDeviceSecret())) {
            log.info("==========设备秘钥错误========");
            return R.fail("设备秘钥错误");
        }

        //4. 激活设备, 也就是连接的时候, 将设备表中的状态由未激活改为已激活, 2激活
        device.setActive(2);
        deviceFeign.update(device);
        log.info("==========设备激活成功========");

        //5. 创建这个设备的mqtt用户到, mqtt_user表中
        mqttUserService.addMqttUser(device.getTenantId());
        log.info("==========开通emqx服务器账号成功========");

        //6. 创建mqtt的topic连接(增量连接)
        deviceTopicService.joinDeviceTopic(device.getDeviceKey());
        log.info("==========增量加载设备Topic成功========");

        //7. 封装mqtt服务器数据到dto返回
        dto.setTenantId(device.getTenantId());
        dto.setUsername(device.getTenantId());
        dto.setPassword(device.getTenantId());
        //tcp://192.168.200.133:1883
        String url = mqttProperties.getUrl();
        //tcp:192.168.200.133:1883
        url = url.replaceAll("//", "");
        //tcp    192.168.200.133    1883
        String[] split = url.split(":");

        dto.setUrl(url);
        dto.setIp(split[1]);
        dto.setPort(Integer.parseInt(split[2]));

        return R.ok(dto);
    }

}
